package com.beiding.hotcode.v2;

import java.net.URL;
import java.net.URLClassLoader;

public class MyClassLoader extends URLClassLoader {

    private interface L {
        Class load(String name) throws ClassNotFoundException;
    }

    private L l;

    public MyClassLoader(URL[] urls) {
        super(urls, null);
        l = name -> {
            try {
                return super.loadClass(name);
            } catch (ClassNotFoundException ignore) {

            }
            return Thread.currentThread().getContextClassLoader().loadClass(name);
        };
    }

    public MyClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, null);
        l = name -> {
            try {
                return super.loadClass(name);
            } catch (ClassNotFoundException ignore) {
            }

            try {
                return parent.loadClass(name);
            } catch (ClassNotFoundException ignore) {
            }
            return Thread.currentThread().getContextClassLoader().loadClass(name);
        };
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        return l.load(name);
    }
}
